const {error} = require("console");
var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const _ = require("lodash");

/* GET users listing. */
router.get("/", function (req, res, next) {
    const filePath = req.query.filePath;
    const response = {
        message: '',
        success: true,
        file: '',
    };
    try {
        const fileName = path.basename(filePath); // 文件名
        const encodedFileName = `${encodeURIComponent(fileName)}`
        console.log("Encoded fileName", encodedFileName);
        res.setHeader("Content-Type", "application/pdf");
        res.setHeader("Content-Disposition", `attachment; filename="${encodedFileName}"`);
        // 读取文件内容并发送给客户端
        const fileStream = fs.createReadStream(filePath);
        fileStream.pipe(res);
    } catch (err) {
        console.error(`Got an error trying to read the file: ${err.message}`);
        res.send("error", error);
    }
});

module.exports = router;
